﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraPanner : MonoBehaviour
{

    private GameObject target;
    public float pannerThreshold = 0.5f;
    public int level=-1;
    public int LevelHeight = 800;
    
    // Start is called before the first frame update
    void Start()
    {
        level = -1;
    }

    // Update is called once per frame
    void Update()
    {
        if (!GameManager.Instance.character)
        {
            return;
        }
        target = GameManager.Instance.character.gameObject;
           
        Vector2 viewportLoc = Camera.main.WorldToViewportPoint(target.transform.position);
        if (viewportLoc.y > pannerThreshold)
        {
            transform.position = Camera.main.ViewportToWorldPoint(
                new Vector3(0.5f, viewportLoc.y - (pannerThreshold - 0.5f), 0f));
            int tempLevel =(int) (transform.position.y / 8);
            if( tempLevel > level){
                level = tempLevel;
                GameManager.Instance.SendChapterChanged(level);
                Debug.LogWarning("level:"+level.ToString());
                if(level >=5){
                    GameManager.Instance.GameWin();
                }
            }
        }
    }
    
    
}
